package community.hospital.service.application.impl;
import com.google.common.collect.Lists;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import community.hospital.convert.CommonConvert;
import community.hospital.convert.DoctorConvert;
import community.hospital.convert.PageConvert;
import community.hospital.entity.Doctor;
import community.hospital.entity.request.DoctorRequest;
import community.hospital.entity.request.ListDoctorRequest;
import community.hospital.entity.response.*;
import community.hospital.service.application.DepartmentApplicationService;
import community.hospital.service.application.DoctorApplicationService;
import community.hospital.service.application.HospitalApplicationService;
import community.hospital.service.application.ScheduleApplicationService;
import community.hospital.service.domain.DoctorService;
import community.hospital.util.RedisUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.List;
import java.util.stream.Collectors;

import static community.hospital.common.RedisConstant.SPLIT;

/**
 * @author yindingjie
 */
@Service
public class DoctorApplicationServiceImpl implements DoctorApplicationService {

    @Resource
    private DoctorService doctorService;

    @Resource
    private DepartmentApplicationService departmentApplicationService;

    @Resource
    private HospitalApplicationService hospitalApplicationService;

    @Resource
    private ScheduleApplicationService scheduleApplicationService;

    @Resource
    private RedisUtil redisUtil;

    @Override
    public Boolean addDoctor(DoctorRequest request) {
        Doctor doctor = DoctorConvert.INSTANCE.convertToDoctor(request);
        return doctorService.save(doctor);
    }

    @Override
    public Page<DoctorResponse> listDoctor(ListDoctorRequest request) {
        Page<Doctor> doctorPage = doctorService.listDoctor(request);
        Page<DoctorResponse> doctorResponsePage = PageConvert.convert(doctorPage, DoctorConvert.INSTANCE::convertToDoctorResponse);
//        for (int i = 0; i < doctorResponsePage.getRecords().size(); i++) {
//            Doctor doctor = doctorPage.getRecords().get(i);
//            DoctorResponse doctorResponse = doctorResponsePage.getRecords().get(i);
//            getDoctorResp(doctor,doctorResponse);
//        }
        return doctorResponsePage;
    }

    @Override
    public Boolean removeDoctor(List<Long> ids) {
        return doctorService.removeByIds(ids);
    }

    @Override
    public Boolean updateDoctor(DoctorRequest request) {
        Doctor doctor = DoctorConvert.INSTANCE.convertToDoctor(request);
        return doctorService.updateById(doctor);
    }

    @Override
    public DoctorResponse queryDoctor(Long id) {
        Doctor doctor = doctorService.getById(id);
        DoctorResponse doctorResponse = DoctorConvert.INSTANCE.convertToDoctorResponse(doctor);
        return doctorResponse;
    }

    @Override
    public List<OptionResp>  listDoctorByCondition(ListDoctorRequest request) {
        List<Doctor> doctorList = doctorService.listDoctorByCondition(request);
        return convert(doctorList);
    }

    private List<OptionResp> convert(List<Doctor> doctorList){
    return     doctorList.stream().map(doctor -> {
            OptionResp optionResp = new OptionResp();
            optionResp.setValue(doctor.getId());
            optionResp.setKey(doctor.getId());
            optionResp.setLabel(doctor.getName());
            optionResp.setTitle(doctor.getName());
            return optionResp;
        }).collect(Collectors.toList());
    }

//    private DoctorResponse getDoctorResp(Doctor doctor,DoctorResponse doctorResponse) {
//        Long departmentId = doctor.getDepartment1Id();
//        doctor.get
//        Long hospitalId = doctor.getHospitalId();
//        DepartmentResponse departmentResponse = departmentApplicationService.queryById(departmentId);
//        HospitalResponse hospitalResponse = hospitalApplicationService.queryById(hospitalId);
//        doctorResponse.setHospitalResponse(hospitalResponse);
//        doctorResponse.setDepartmentResponse(departmentResponse);
////        ScheduleResponse scheduleResponse = scheduleApplicationService.querySchedule(doctor.getId(), LocalDate.now());
////        String key = String.join(SPLIT, "order", scheduleResponse.getId().toString(), LocalDate.now(ZoneId.systemDefault()).toString());
////        Integer remainQuantity = redisUtil.get(key, () -> doctor.getRegisterQuantity(), 24 * 60 * 60);
////        doctorResponse.setRemainingQuantity(remainQuantity);
//        return doctorResponse;
//    }
}
